<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/29/029
 * Time: 11:35
 */

namespace app\api\controller\v1;

//use app\api\controller\BaseController;
use app\api\controller\BaseController;
use app\api\service\WxNotify;
use app\api\validate\IDMustBePostiveInt;
use app\api\service\Pay as PayService;
use app\api\service\Alipay as AlipayService;
use app\api\validate\PayValidate;
use app\lib\exception\SuccessMessage;

include_once  __DIR__ . '/../../../../extend/WxPay/WxPay.Api.php';

class Pay extends BaseController {

    /**
     * 获取支付页面数据
     * @param $model
     * @param $id
     * @return array
     */
    public function payDate($model, $id) {
        (new IDMustBePostiveInt())->goCheck();
        (new PayValidate())->goCheck('pay');
        return PayService::pay_data($model, $id);
    }

    /**
     * 获取微信支付数据
     * @param $model
     * @param $id
     * @param int $use_clothes_money
     * @return array
     */
    public function getWxpayData($model, $id, $use_clothes_money = 0)
    {
        (new IDMustBePostiveInt())->goCheck();
        (new PayValidate())->goCheck('pay');
        $data = PayService::wx_app_pay($model, $id, $use_clothes_money);
        file_put_contents('../runtime/pay.txt', json($data));
        return $data;
    }

    /**
     * 获取支付宝支付数据
     * @param $model
     * @param $id
     * @param int $use_clothes_money
     * @return string
     */
    public function getAlipayData($model, $id, $use_clothes_money = 0) {
        (new IDMustBePostiveInt())->goCheck();
        (new PayValidate())->goCheck('pay');
        $pay_data = AlipayService::getAlipayDate($model, $id, $use_clothes_money);
        return $pay_data;
    }

    /**
     * 微信支付回调页面
     */
    public function notify()
    {
        file_put_contents('wxpay.txt', "回调来了 \n");
        $data = $GLOBALS['HTTP_RAW_POST_DATA'];
        $notify = new \WxPayNotify();
        $notify->Handle(false);
    }

    /**
     * 支付宝回调方法
     */
    public function aliNotify() {
        $data = input('param.');

        file_put_contents('alipay.txt', "回调来了 \n" . json_encode($data));
    }

    /**
     * 站内支付
     * @return \think\response\Json
     */
    public function inStationPay() {
        (new IDMustBePostiveInt())->goCheck();
        (new PayValidate())->goCheck('in_station_pay');
        $model = input('param.in_station_pay_model');
        $id = input('param.id');
        PayService::inStationPay($id, $model);
        return json(new SuccessMessage(), 201);
    }
}